package com.kapcb.mvc.filter;

import com.kapcb.context.core.ApplicationThreadContextHolder;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

/**
 * <a>Title: ApplicationThreadContextClearFilter </a>
 * <a>Author: kapcb <a>
 * <a>Description: ApplicationThreadContextClearFilter <a>
 *
 * @author Kpacb
 * @version 1.0
 * @date 2024/5/26 5:30
 * @since 1.0
 */
public class ApplicationThreadContextClearFilter implements Filter {

    /**
     * 框架默认的线程上下文清理过滤器，请求结束后主动清理线程上下文，避免内存泄漏
     *
     * @param request
     * @param response
     * @param filterChain
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        try {
            filterChain.doFilter(request, response);
        } finally {
            ApplicationThreadContextHolder.clearContext();
        }
    }

}
